<!-- 充电站管理 -->
<template>
  <div class="app">
    <div class="page-header">
      <i class="el-icon-time"></i>
      <span>充电站管理</span>
    </div>

    <div class="search-bar">
      <el-input
        v-model="param.queryParam"
        placeholder="请输入关键字搜索"
        class="search-input"
        clearable
        prefix-icon="el-icon-search"
      >
      </el-input>
      <div class="button-group">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          @click="queryChargingStationBtn"
          >查询</el-button
        >
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd"
          >新增</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          @click="handleBatchDelete"
          :disabled="!selectedData.length"
          >删除</el-button
        >
      </div>
    </div>

    <!-- 
        :data="tableData"，绑定实例中的tableData数组作为表格的数据源 
        表格显示数据步骤:
        1：用表格的data属性绑定显示数据
        2：用列标签的prop属性绑定数据对象的属性
        3：对表格的数据个性化处理(转换、加颜色)，用插槽获取这行数据

        @selection-change：多选框触发的事件
    -->

    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
      v-loading="loading"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="id"
        label="序号"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stationNumber"
        label="充电桩编号"
        min-width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stationName"
        label="充电站名称"
        min-width="180"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stationType"
        label="充电站类型"
        min-width="180"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stationLocation"
        label="电站位置"
        min-width="160"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="maxChargerCount"
        label="最大充电桩数目"
        min-width="160"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="stationManager"
        label="电站负责人"
        min-width="160"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        min-width="160"
        align="center"
      >
        <!-- 使用作用域插槽自定义列内容 -->
        <template slot-scope="scope">
          <span :style="{ color: scope.row.status === 1 ? 'green' : 'red' }">
            {{ scope.row.status === 1 ? "营业中" : "暂停营业" }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        :page-sizes="[3, 5, 10, 30]"
        :page-size="pageSize"
        :current-page="pageNum"
        layout="total, sizes, prev, pager, next"
        :total="total"
        @next-click="nextPage"
        @prev-click="prevPage"
        @current-change="changePage"
        @size-change="changeSize"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "app",
  data() {
    return {
      param: {}, // 搜索栏里的条件
      tableData: [], // 下方显示的数据
      currentPage: 1,
      pageSize: 10,
      pageNum: 1,
      total: 0,
      selectedData: [], // 多选框选中的数据
      loading: false,
    };
  },
  methods: {
    nextPage(val) {
      // 下一页  回调参数:val  当前页
      this.pageNum = val;
      this.queryChargingStation();
    },
    prevPage(val) {
      this.pageNum = val;
      this.queryChargingStation();
    },
    changePage(val) {
      this.pageNum = val;
      this.queryChargingStation();
    },
    changeSize(val) {
      this.pageNum = 1; // 改变pageSize就从第一页开始查
      this.pageSize = val;
      this.queryChargingStation();
    },
    queryChargingStationBtn() {
      // 查询按钮
      this.pageNum = 1;
      this.queryChargingStation();
    },
    queryChargingStation() {
      // 查询
      // 查询充电桩信息，分页条件pageNum，pageSize
      let pageData = {};
      pageData.pageNum = this.pageNum;
      pageData.pageSize = this.pageSize;
      pageData.param = this.param;

      this.$axios
        .post("api/chargingStationManage/queryChargingStation", pageData)
        .then((resp) => {
          console.log(resp.data);
          if (resp.data.code == 200) {
            this.tableData = resp.data.data.list;
            this.total = resp.data.data.total;
          }
        });
    },
    // 多选框触发的事件
    handleSelectionChange(val) {
      // 回调参数val
      // 多选框触发点击事件         val：选中的对象数组
      // console.log(val);
      this.selectedData = val;
    },
    handleAdd() {
      // 新增方法
    },
    handleBatchDelete() {
      // 批量删除
      this.$confirm("是否确定删除数据", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 发送删除请求
          // 选中的id数据
          let ids = [];
          this.selectedData.forEach((e) => {
            ids.push(e.id);
          });

          this.$axios
            .get("api/chargingStationManage/batchDel/" + ids)
            .then((resp) => {
              if (resp.data.code == 200) {
                this.queryChargingStationBtn(); // 重新查一遍
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleDelete(id) {
      // 单删
      this.$confirm("是否确定删除数据", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .get("api/chargingStationManage/batchDel/" + id)
            .then((resp) => {
              if (resp.data.code == 200) {
                this.queryChargingStationBtn(); // 重新查一遍
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleEdit(id) {
      // 编辑按钮
      // 使用 $router.push 方法进行路由跳转并传递 id 参数
      this.$router.push({ name: "ChargingStationManageEdit", params: { id } });
    },
  },
  created() {
    this.queryChargingStationBtn(); // 开始的时候查一次
  },
};
</script>

<style scoped>
.appointment-manage {
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.page-header {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.page-header i {
  font-size: 24px;
  margin-right: 10px;
}

.search-bar {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-input {
  width: 300px;
}

.button-group {
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

:deep(.el-table) {
  margin-top: 15px;
}

:deep(.el-button--text) {
  padding: 0 8px;
}

:deep(.el-button--text + .el-button--text) {
  margin-left: 0;
}

:deep(.el-tag) {
  text-transform: uppercase;
}
</style>
